<!--<!DOCTYPE html>-->
<html lang="en">
<head>
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=no" name="viewport">
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        html{
            cursor: url("苍蝇拍1(1).png"),auto;
        }
        body{
            overflow:hidden;
        }
        img{
            height: 60%;

        }
    </style>
</head>
<body>

<div>游戏得分：<span id="score">0</span> 分

<input type="button" id="startBtn" onclick="startGame()" value="开始游戏" />
</div>
</body>
<script>
    var bgMusic ;
    function startGame(){
        startBtn.style.display = "none";
        bgMusic= new Audio();
        bgMusic.src = "bg.mp3";
        bgMusic.loop = true;
        bgMusic.play();
        f();
    }


    window.onload = function(){
        
    };

    function f(){
        var cangying = document.createElement("img");
        cangying.src = "http://5b0988e595225.cdn.sohucs.com/images/20180626/7c528caec0154a5d92821fcc4709d5aa.gif";

        var randTop = Math.floor(Math.random()*400)+50;

        cangying.style.position = "absolute";
        cangying.style.left = "1000px";
        cangying.style.top = randTop+"px";

        cangying.onclick = function(){
            var hitMusic = new Audio();
            hitMusic.src = "hit.mp3";
            hitMusic.play();
            score.innerHTML = parseInt(score.innerHTML) + 10;

            clearInterval(timer); // 停止定时器
            document.body.removeChild(cangying); // 删掉蝴蝶

            if(parseInt(score.innerHTML)==100){
                bgMusic.pause();//暂停
                var music = new Audio();
                music.src = "win.mp3";
                music.play();
                alert("你好厉害！");
                return;
            }

            f(); //重新来过
        };

        document.body.appendChild(cangying);

        var timer = setInterval(function(){
            if(parseInt(cangying.style.left)<-100){
                bgMusic.pause();//暂停
                var music = new Audio();
                music.src = "fail.mp3";
                music.play();

                clearInterval(timer);
                document.body.removeChild(cangying);
                alert("游戏失败！");
                /*
                f();
                */
                return;
            }
            cangying.style.left = parseInt(cangying.style.left) - 3;
        },10);
    }

</script>

</html>